package remote_work.core.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import remote_work.dto.InputObject;
import remote_work.dto.OutputObject;
import remote_work.util.DBHelper;

/**
 * @author LiuPing
 *
 * 2018年4月8日
 */
public class CommonService {
	
	/**
	 * 获取公共参数
	 * 
	 * @param inputObject
	 * @return
	 * @throws Exception
	 */
	public OutputObject getCommon(InputObject inputObject) throws Exception {
		OutputObject outputObject = new OutputObject();
		DBHelper db = new DBHelper();
		List<Object> params = new ArrayList<>();
		String querySql = "select param_name,param_value from TB_PUB_PARAMETER where type = ?";
		params.add(inputObject.getValue("type"));
		if (inputObject.getValue("name") != null && "".equals(inputObject.getValue("name"))) {
			querySql += " and PARAM_NAME = ?";
			params.add(inputObject.getValue("name"));
		}
		outputObject.setBeans(db.find(querySql, params));
		return outputObject;
	}
	
	/**
	 * 添加公共参数
	 * 
	 * @param inputObject
	 * @return
	 * @throws Exception
	 */
	public OutputObject addCommon(InputObject inputObject) throws Exception {
		OutputObject outputObject = new OutputObject();
		DBHelper db = new DBHelper();
		String insertSql = "insert into TB_PUB_PARAMETER (PARAM_CODE, PARAM_NAME, PARAM_VALUE, DESCRIPTION, TYPE) values (?, ?, ?, ?, ?)";
		List<Object> params = new ArrayList<>();
		params.add(inputObject.getValue("code"));
		params.add(inputObject.getValue("name"));
		params.add(inputObject.getValue("value"));
		params.add(inputObject.getValue("desc"));
		params.add(inputObject.getValue("type"));
		int doUpdate = db.doUpdate(insertSql, params);
		if (doUpdate > 0) {
			outputObject.setReturnCode("0");
		} else {
			outputObject.setReturnCode("-1");
		}
		return outputObject;
	}
	
	
	public OutputObject deleteDept(InputObject inputObject) throws Exception {
		OutputObject outputObject = new OutputObject();
		DBHelper db = new DBHelper();
		String selSql ="select * from  TB_PUB_PARAMETER where PARAM_NAME=?";
		List<Object> params = new ArrayList<>();
		params.add(inputObject.getValue("param_value"));
		List<Map<String, Object>> list = db.find(selSql, params);
		if(list.size()>0){
			outputObject.setReturnCode("-1");
			outputObject.setReturnMessage("该系别存在专业，请先删除相关专业！");
			return outputObject;
		}
		
		String sql ="delete from  TB_PUB_PARAMETER where PARAM_VALUE=?";
		
		int doUpdate = db.doUpdate(sql, params);
		outputObject.setReturnCode(String.valueOf(doUpdate));
		
		if(doUpdate<=0){
			outputObject.setReturnMessage("删除失败！");
		}
		return outputObject;
	}
	
	public OutputObject deleteMCC(InputObject inputObject) throws Exception {
		OutputObject outputObject = new OutputObject();
		DBHelper db = new DBHelper();
		//String selSql ="select * from  TB_PUB_PARAMETER where PARAM_NAME = ?";
		List<Object> params = new ArrayList<>();
		params.add(inputObject.getValue("param_value"));
		System.out.println("专业班级或者课程删除"+inputObject.getValue("param_value"));
		//List<Map<String, Object>> list = db.find(selSql, params);
		//if(list.size()>0){
		//	System.out.println("返回参数code"+"-1");
		////	outputObject.setReturnCode("-1");
		//	outputObject.setReturnMessage("删除失败！");
		//	return outputObject;
		//}
		String sql ="delete from  TB_PUB_PARAMETER where PARAM_VALUE=?";
		
		int doUpdate = db.doUpdate(sql, params);
		System.out.println("返回参数code"+doUpdate);
		outputObject.setReturnCode(String.valueOf(doUpdate));
		System.out.println("返回参数code"+outputObject.getReturnCode());
		return outputObject;
	}
}
